﻿namespace PowerSong.UserInterface
    {
    using System;
    using System.Net;
    using System.Reflection;
    using System.Text;
    using System.Windows;

    /// <summary>
    /// Interaction logic for ExceptionWindow.xaml
    /// </summary>
    public partial class ExceptionWindow : Window
        {

        public ExceptionWindow(Exception ex, string messageAtTop = "")
            {

            InitializeComponent();

            // Show the message
            if (string.IsNullOrEmpty(messageAtTop))
                {
                MessageTextBlock.Text = ex.Message;
                }
            else
                {
                MessageTextBlock.Text = messageAtTop;
                }

            // Add additional details
            var additionalDetails = "[" + DateTime.Now.ToString() + "]";
            if (messageAtTop != ex.Message) additionalDetails += Environment.NewLine + ex.Message;
            if (!string.IsNullOrEmpty(messageAtTop)) additionalDetails += Environment.NewLine + "(" + messageAtTop + ")";
            additionalDetails += Environment.NewLine + Environment.NewLine + "Stack Trace:" + Environment.NewLine + ex.StackTrace;

            // Write additional details into the text box
            AdditionalDetailsTextBox.Text = additionalDetails;

            }

        private void ContinueButton_Click(object sender, RoutedEventArgs e)
            {
            this.DialogResult = true;
            }

        private void SendButton_Click(object sender, RoutedEventArgs e)
            {

            try
                {

                // What version are we running?
                var version = Assembly.GetExecutingAssembly().GetName().Version.ToString();

                // Put the data together into a request object
                var request = HttpWebRequest.Create(Infrastructure.ReportErrorUrl);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                var postData = "txtSource=" + "PowerSong " + version + "&txtDetails=" + FeedbackWindow.EncodeText(AdditionalDetailsTextBox.Text);
                var data = (new ASCIIEncoding()).GetBytes(postData);
                request.ContentLength = data.Length;

                // Send the data
                var requestStream = request.GetRequestStream();
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();

                // Close the form
                if (Utilities.Confirm("Feedback",
                                      "The details of the error have been sent. Thank you very much!" + Environment.NewLine + Environment.NewLine + 
                                      "If you would like us to try to fix this problem, please tell us exactly what you were trying to do using the feedback mechanismin the Help menu." + Environment.NewLine + Environment.NewLine +
                                      "Would you like to do this now?"))
                    {
                    new FeedbackWindow().ShowDialog();
                    }

                // Close the window
                this.DialogResult = true;

                }
            catch (Exception ex)
                {
                Utilities.ShowErrorMessage("Communications Error", "The details of the error could not be sent, but thanks for trying anyway!", ex);
                }

            }

        }

    }
